


#ifndef PF_INSERT_H
#define PF_INSERT_H

#include "pf_entitycontainer.h"
#include "lx_Export.h"

class PF_BlockList;

/**
 * Holds the data that defines an insert.
 */
class LX_EXPORT PF_InsertAttribute {
public:
	/**
	 * Default constructor.
	 */
    PF_InsertAttribute() {}

    /**
     * @param name The name of the block used as an identifier.
     * @param insertionPoint Insertion point of the block.
     * @param scaleFactor Scale factor in x / y.
     * @param angle Rotation angle.
     * @param cols Number of cols if we insert a whole array.
     * @param rows Number of rows if we insert a whole array.
     * @param spacing Spacing between rows and cols.
     * @param blockSource Source for the block to insert if other than parent.
     *    Normally blocks are requested from the entity's parent but the
     *    block can also come from another resource. PF_Text uses that
     *    to share the blocks (letters) from a font.
     * @param updateMode LX_Define::Update will update the insert entity instantly
     *    LX_Define::NoUpdate will not update the insert. You can update
     *	  it later manually using the update() method. This is 
     *    often the case since you might want to adjust attributes
     *    after creating an insert entity.
     */
    PF_InsertAttribute(const QString& name,
                  PF_Vector insertionPoint,
                  PF_Vector scaleFactor,
                  double angle,
                  int cols, int rows, PF_Vector spacing,
                  PF_BlockList* blockSource = NULL,
                  LX_Define::UpdateMode updateMode = LX_Define::Update) {
        this->name = name;
        this->insertionPoint = insertionPoint;
        this->scaleFactor = scaleFactor;
        this->angle = angle;
        this->cols = cols;
        this->rows = rows;
        this->spacing = spacing;
        this->blockSource = blockSource;
        this->updateMode = updateMode;

    }

    friend class PF_Insert;

    friend std::ostream& operator << (std::ostream& os,
                                      const PF_InsertAttribute& d) {
        os << "(" << d.name.toLatin1().data() << ")";
        return os;
    }

    QString name;
    PF_Vector insertionPoint;
    PF_Vector scaleFactor;
    double angle;
    int cols, rows;
    PF_Vector spacing;
    PF_BlockList* blockSource;
    LX_Define::UpdateMode updateMode;
};



/**
 * An insert inserts a block into the drawing at a certain location
 * with certain attributes (angle, scale, ...). 
 * Inserts don't really contain other entities internally. They just
 * refer to a block. However, to the outside world they act exactly 
 * like EntityContainer.
 *

 */
class LX_EXPORT PF_Insert : public PF_Container {
public:
    PF_Insert(PF_Container* parent,
              const PF_InsertAttribute& d);
    virtual ~PF_Insert();

    virtual PF_Entity* clone() {
        PF_Insert* i = new PF_Insert(*this);
        i->setOwner(isOwner());
        i->initId();
        i->detach();
        return i;
    }

    /** @return LX_Define::EntityInsert */
    virtual LX_Define::EntityType rtti() const {
        return LX_Define::EntityInsert;
    }

    /** @return Copy of data that defines the insert. **/
    PF_InsertAttribute getData() const {
        return data;
    }

	/**
	 * Reimplementation of reparent. Invalidates block cache pointer.
	 */
    virtual void reparent(PF_Container* parent) {
		PF_Entity::reparent(parent);
		block = NULL;
    }

    PF_Block* getBlockForInsert();

    virtual void update();

    QString getName() const {
        return data.name;
    }

        void setName(const QString& newName) {
		data.name = newName;
		update();
	}

    PF_Vector getInsertionPoint() const {
        return data.insertionPoint;
    }
    void setInsertionPoint(const PF_Vector& i) {
        data.insertionPoint = i;
    }

    PF_Vector getScale() const {
        return data.scaleFactor;
    }

	void setScale(const PF_Vector& s) {
		data.scaleFactor = s;
	}

    double getAngle() const {
        return data.angle;
    }
    void setAngle(double a) {
        data.angle = a;
    }

    int getCols() const {
        return data.cols;
    }
	void setCols(int c) {
		data.cols = c;
	}

    int getRows() const {
        return data.rows;
    }
	void setRows(int r) {
		data.rows = r;
	}

    PF_Vector getSpacing() const {
        return data.spacing;
    }
	void setSpacing(const PF_Vector& s) {
		data.spacing = s;
	}
	
    virtual bool isVisible();

    virtual PF_VectorSolutions getRefPoints();
    virtual PF_Vector getNearestRef(const PF_Vector& coord,
                                     double* dist = NULL);

    virtual void move(PF_Vector offset);
    virtual void rotate(PF_Vector center, double angle);
    virtual void scale(PF_Vector center, PF_Vector factor);
    virtual void mirror(PF_Vector axisPoint1, PF_Vector axisPoint2);

    friend std::ostream& operator << (std::ostream& os, const PF_Insert& i);

protected:
    PF_InsertAttribute data;
	PF_Block* block;
};


#endif
